package com.ljf.daily.makemoneysoftware.infra.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.infra.repository.impl.BasisRepositoryImpl;
import com.ljf.common.utils.CollectionUtil;
import com.ljf.common.utils.StringUtil;
import com.ljf.daily.makemoneysoftware.domain.repository.DailyDatMakeMoneySoftwareRepository;
import com.ljf.daily.makemoneysoftware.dto.query.DailyDatMakeMoneySoftwareQuery;
import com.ljf.daily.makemoneysoftware.infra.mapper.DailyDatMakeMoneySoftwareMapper;
import com.ljf.daily.makemoneysoftware.model.DailyDatMakeMoneySoftware;
import com.ljf.daily.makemoneysoftware.vo.DailyDatMakeMoneySoftwareVO;
import com.ljf.finance.domain.repository.FinanceDatVirtualAccountRepository;
import com.ljf.finance.dto.query.FinanceDatVirtualAccountQuery;
import com.ljf.finance.model.FinanceDatVirtualAccount;
import com.ljf.finance.vo.FinanceDatVirtualAccountVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 赚钱软件定义  数据访问接口实现
 *
 * @author LJF
 * @since 2024-01-31
 */
@Component
public class DailyDatMakeMoneySoftwareRepositoryImpl extends BasisRepositoryImpl<DailyDatMakeMoneySoftwareMapper, DailyDatMakeMoneySoftware, DailyDatMakeMoneySoftwareQuery, DailyDatMakeMoneySoftwareVO> implements DailyDatMakeMoneySoftwareRepository {

    @Resource
    private FinanceDatVirtualAccountRepository financeDatVirtualAccountRepository;

    @Override
    protected QueryWrapper<DailyDatMakeMoneySoftware> fillWrapper(DailyDatMakeMoneySoftwareQuery query, boolean sort) throws ServiceException {
        QueryWrapper<DailyDatMakeMoneySoftware> wrapper = super.fillWrapper(query, sort);
        if (CollectionUtil.isNotEmpty(query.getNameList())) {
            wrapper.in(DailyDatMakeMoneySoftware.COL_NAME, query.getNameList());
        }
        if (CollectionUtil.isNotEmpty(query.getRegisterPhoneList())) {
            wrapper.in(DailyDatMakeMoneySoftware.COL_REGISTER_PHONE, query.getRegisterPhoneList());
        }
        if (CollectionUtil.isNotEmpty(query.getCodeList())) {
            wrapper.in(DailyDatMakeMoneySoftware.COL_CODE, query.getCodeList());
        }
        if (StringUtil.isNotEmpty(query.getNameLike())) {
            wrapper.like(DailyDatMakeMoneySoftware.COL_NAME, query.getNameLike());
        }
        return wrapper;
    }

    @Override
    protected void fillInfo(List<DailyDatMakeMoneySoftwareVO> resultList) throws ServiceException {
        super.fillInfo(resultList);
        if (CollectionUtil.isNotEmpty(resultList)) {
            List<String> accountCodeList = resultList.stream().map(DailyDatMakeMoneySoftwareVO::getAccountCode).distinct().collect(Collectors.toList());
            List<FinanceDatVirtualAccountVO> accountList = financeDatVirtualAccountRepository.selectList(FinanceDatVirtualAccountQuery.builder().codeList(accountCodeList).build());
            Map<String, String> accountMap = accountList.stream().collect(Collectors.toMap(FinanceDatVirtualAccount::getCode, FinanceDatVirtualAccount::getName, (a, b) -> a));
            resultList.forEach(one -> one.setAccountName(accountMap.getOrDefault(one.getAccountCode(), "")));
        }
    }
}
